# 示例：简单的端口扫描脚本
import socket

# 定义要扫描的IP地址,这里使用百度的网址
target = "www.baidu.com"

# 定义要扫描的端口范围
ports = [21, 22, 80, 443]

# 开始扫描
for port in ports:
    # 创建socket对象
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 设置超时时间
    s.settimeout(1)
    # 尝试连接
    result = s.connect_ex((target, port))
    # 如果连接成功，打印信息
    if result == 0:
        print("Port {} is open".format(port))
    else:  # 连接失败，打印信息
        print("Port {} is closed or filtered".format(port))
        print("The socket number returned by the port {} is {}".format(port, result))
    # 关闭socket对象
    s.close()